package com.aaa.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: upload
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/7/27 16:15
 * @Version:1.0
 **/
@RestController
@RequestMapping("backuser")
public class upload {
    @Value("${filepath}")
    String filepath;
    @PostMapping("/up")
    public String upload(@RequestParam("head")MultipartFile head){
        String cc="";
        String originalFilename = head.getOriginalFilename();
        System.out.println(originalFilename);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHssmm");
        String format = now.format(formatter);
//        文件格式
        if(originalFilename.lastIndexOf(".")!=-1){

            String n1=originalFilename.substring(originalFilename.lastIndexOf("."));
            if(!"".equals(originalFilename)){
                String s = filepath + format + n1;

                File file = new File(s);
                if (!file.exists()) {//如果目录不存在，创建这样一个目录；
                    file.mkdir();
                }
                try {
                    head.transferTo(file);
                    cc=s;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return cc;
    }
//    @PostMapping("/upload")
//    public String[] multiUpload(@RequestParam("head")MultipartFile[] head){
//        String[] arr=new String[head.length];
//        System.out.println("文件的个数:"+head.length);
//        int i=0;
//        for (MultipartFile f : head){
//            String originalFilename = f.getOriginalFilename();
//            String n1=originalFilename.substring(originalFilename.lastIndexOf("."));
//
//            LocalDateTime now = LocalDateTime.now();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHssmm");
//            String format = now.format(formatter);
//            if(!"".equals(originalFilename)){
//                String s =  format+System.currentTimeMillis()+ n1;
//                File file = new File(filepath +s);
//                try {
//                    f.transferTo(file);
//                    arr[i]=s;
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            System.out.println("arr["+i+"]--------"+arr[i]);
//            i++;
//        }
//        return arr;
//    }
//    文件下载
    @RequestMapping("download")
    public void download(HttpServletResponse response) throws Exception {
        // 文件地址，真实环境是存放在数据库中的
        File file = new File("D:/file.txt");
        // 穿件输入对象
        FileInputStream fis = new FileInputStream(file);
        // 设置相关格式
        response.setContentType("application/force-download");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + "a.txt");
        // 创建输出对象
        OutputStream os = response.getOutputStream();
        // 常规操作
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        fis.close();
    }

}
